package com.xerland.tbootstrap.ui.presenters;

import android.accounts.OperationCanceledException;
import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;

import com.xerland.tbootstrap.BootstrapService;
import com.xerland.tbootstrap.BootstrapServiceProvider;
import com.xerland.tbootstrap.core.DataManager;
import com.xerland.tbootstrap.injection.ActivityContext;
import com.xerland.tbootstrap.ui.base.BasePresenter;
import com.xerland.tbootstrap.ui.views.MainActivityView;
import com.xerland.tbootstrap.utils.SafeAsyncTask;

import javax.inject.Inject;

import rx.Subscription;

public class MainActivityPresenter extends BasePresenter<MainActivityView> {
    @Inject
    BootstrapServiceProvider serviceProvider;
    private Context context;
    private DataManager dataManager;

    private Subscription subscription;
    private boolean userHasAuthenticated = false;

    @Inject
    public MainActivityPresenter(@NonNull @ActivityContext Context context,
                                 @NonNull DataManager dataManager) {
        this.context = context;
        this.dataManager = dataManager;
    }

    @Override
    public void attach(MainActivityView mvpView) {
        super.attach(mvpView);
    }

//    public void loadQuotes() {
//        if (NetworkStateChecker.isNetworkAvailable(context)) {
//            Timber.d("startLoadQuotesService! ");
//            context.startService(new Intent(context, LoadEmojisService.class));
//        } else {
//            if (isViewAttached()) getMvpView().showInternetNotAvailable();
//        }
//    }

    public void checkAuth() {
        new SafeAsyncTask<Boolean>() {

            @Override
            public Boolean call() throws Exception {
                final BootstrapService svc = serviceProvider.getService((Activity)context);
                return svc != null;
            }

            @Override
            protected void onException(final Exception e) throws RuntimeException {
                super.onException(e);
                if (e instanceof OperationCanceledException) {
                    // User cancelled the authentication process (back button, etc).
                    // Since auth could not take place, lets finish this activity.
//                    finish();
                }
            }

            @Override
            protected void onSuccess(final Boolean hasAuthenticated) throws Exception {
                super.onSuccess(hasAuthenticated);
                userHasAuthenticated = true;
                getMvpView().showMainView();
//                initScreen();
            }
        }.execute();
    }


//    public void getQuotes() {
//        subscription = dataManager.getQuotes()
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(quoteEntities -> {
//                    if (! quoteEntities.isEmpty()) getMvpView().showQuotes(quoteEntities);
//                    else getMvpView().showQuotesIsEmpty();
//                });
//    }


    @Override
    public void detach() {
        super.detach();
        if (subscription != null && !subscription.isUnsubscribed()) subscription.unsubscribe();
    }
}